
#include "QOpenCVWidget.h"

// Constructor
QOpenCVWidget::QOpenCVWidget(QWidget *parent) : QWidget(parent) {
    if( !face_cascade.load(  "/usr/share/opencv/haarcascades/haarcascade_frontalface_alt.xml" ) ){ printf("--(!)Error loading\n"); exit; };
    if( !eyes_cascade.load( "/usr/share/opencv/haarcascades/haarcascade_eye_tree_eyeglasses.xml" ) ){ printf("--(!)Error loading\n"); exit; };
     paint = new QPainter;
     bill =new QPixmap("./billface.png",0,Qt::AutoColor);



}


QOpenCVWidget::~QOpenCVWidget(void) {
    
}

void QOpenCVWidget::putImage(IplImage *cvimage) {

    int cvIndex, cvLineStart;
    // switch between bit depths
    switch (cvimage->depth) {
        case IPL_DEPTH_8U:
            switch (cvimage->nChannels) {
                case 3:
                    if ( (cvimage->width != image.width()) || (cvimage->height != image.height()) ) {
                        QImage temp(cvimage->width, cvimage->height, QImage::Format_RGB32);
                        image = temp;
                    }
                    cvIndex = 0; cvLineStart = 0;
                    for (int y = 0; y < cvimage->height; y++) {
                        unsigned char red,green,blue;
                        cvIndex = cvLineStart;
                        for (int x = 0; x < cvimage->width; x++) {
                            // DO it
                            red = cvimage->imageData[cvIndex+2];
                            green = cvimage->imageData[cvIndex+1];
                            blue = cvimage->imageData[cvIndex+0];
                            
                            image.setPixel(x,y,qRgb(red, green, blue));
                            cvIndex += 3;
                        }
                        cvLineStart += cvimage->widthStep;                        
                    }
                    break;
                default:
                    printf("This number of channels is not supported\n");
                    break;
            }
            break;
        default:
            printf("This type of IplImage is not implemented in QOpenCVWidget\n");
            break;
    }
    //imagelabel->setPixmap(QPixmap::fromImage(image));
    frame = cv::Mat(cvimage,false);

    update();


}
void QOpenCVWidget::detectAndDisplay()
{
        std::vector<Rect> faces;
        Mat frame_gray;

        cvtColor( frame, frame_gray, CV_BGR2GRAY );
        equalizeHist( frame_gray, frame_gray );
        double deb[4];

        //-- Detect faces
        face_cascade.detectMultiScale( frame_gray, faces, 1.1, 2, 0, Size(80, 80) );

        for( size_t i = 0; i < faces.size(); i++ )
         {
           Mat faceROI = frame_gray( faces[i] );
           std::vector<Rect> eyes;

           //-- In each face, detect eyes
           eyes_cascade.detectMultiScale( faceROI, eyes, 1.1, 2, 0 |CV_HAAR_SCALE_IMAGE, Size(30, 30) );
           if( eyes.size() == 2)
           {
               deb[0]=((double)faces[i].x)*0.9;
               deb[1]=((double)faces[i].y)*0.9;
               deb[2]=((double)faces[i].width)*1.2;
               deb[3]=((double)faces[i].height)*1.2;

               paint->drawPixmap((int)deb[0],(int)deb[1],(int)deb[2],(int)deb[3],*bill);
               if(!button_cv_parent->isEnabled())
               {
                  /* QPixmap screenshot;
                   screenshot = QPixmap::grabWidget(this,0,0,this->width(),this->height());
                   screenshot.save("./test.png","PNG");
                   */

                   button_cv_parent->setDisabled(false);

                   qDebug("catch");

               }


            }


         }

        if(faces.size()>0)
        {
            paint->end();
            /*QPixmap screenshot;
            screenshot = QPixmap::grabWidget(this,0,0,this->width(),this->height());
            screenshot.save("./test.png","PNG"); */
              qDebug("draw");
            return;
        }

        paint->end();





}
void QOpenCVWidget::paintEvent(QPaintEvent *)
{
    paint->begin( this );
    paint->drawImage(0,0,image);

    if( !frame.empty() )
    {
        detectAndDisplay();
        return;
    }

    paint->end();


}
